package com.yudean.esb.config;


import cn.hutool.core.util.ArrayUtil;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.yudean.esb.service.*;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;


@Configuration
public class CxfConfig {

    @Resource
    private Bus bus;

    @Resource
    private MdsIntIndexService mdsIntIndexService;
    @Resource
    private MdsArCustomersService mdsArCustomersService;
    @Resource
    private ERPPLNService erpplnService;
    @Resource
    private ERPInsuranceService erpInsuranceService;
    @Resource
    private ErpIdsInsHeaderIntService erpIdsInsHeaderIntService;
    @Resource
    private ERPIndexService erpIndexService;
    @Resource
    private ERPFinPerforService erpFinPerforService;
    @Resource
    private ErpControlService erpControlService;
    @Resource
    private PutService putService;
    @Resource
    private ERPPutService erpPutService;
    @Resource
    private ErpIdsInvTransactionIntPutService erpIdsInvTransactionIntPutService;
    @Resource
    private AccountDAO accountDAO;

    @Bean("jsonProvider")
    public JacksonJsonProvider getJacksonJsonProvider() {
        return new JacksonJsonProvider();
    }

    /**
     *  ERP财务信息服务
     */
    @Bean
    public Endpoint erpFinanceService(){
        EndpointImpl endpoint = new EndpointImpl(bus,accountDAO);
        endpoint.publish("/services/ERPFinanceService");
        return endpoint;
    }


    /**
     *  ERP预算信息服务
     */
    @Bean
    public Endpoint erpPLNService(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpplnService);
        endpoint.publish("/services/ERPPLNService");
        return endpoint;
    }

    /**
     *  ERP统计信息服务
     */
    @Bean
    public Endpoint erpIndexWebService(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpIndexService);
        endpoint.publish("/services/ERPIndexService");
        return endpoint;
    }

    /**
     *  ERP财务指标数据服务
     */
    @Bean
    public Endpoint mdsIntIndexWebService(){
        EndpointImpl endpoint = new EndpointImpl(bus,mdsIntIndexService);
        endpoint.publish("/services/MdsIntIndexService");
        return endpoint;
    }

    /**
     *  获取用户信息（已不再使用，被通用数据接口取代）
     */
    @Bean
    public Endpoint endpointCustomer(){
        EndpointImpl endpoint = new EndpointImpl(bus,mdsArCustomersService);
        endpoint.publish("/services/ErpArCustomersService");
        return endpoint;
    }

    /**
     *  财务经营数据服务
     */
    @Bean
    public Endpoint erpFinPerformanceWebService(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpFinPerforService);
        endpoint.publish("/services/ERPFinPerformanceService");
        return endpoint;
    }

    /**
     * 天鑫理赔系统通过调用公共接口平台的webservice读取保单数据
     */
    @Bean
    public Endpoint soapInsHeaderInt(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpIdsInsHeaderIntService);
        endpoint.publish("/services/ErpIdsInsHeaderIntService");
        return endpoint;
    }

    /**
     * 获取controlId
     */
    @Bean
    public Endpoint soapControlService(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpControlService);
        endpoint.publish("/services/ErpControlService");
        return endpoint;
    }


    // 统一数据推送接口
    @Bean
    public Endpoint soapPutService(){
        EndpointImpl endpoint = new EndpointImpl(bus,putService);
        endpoint.publish("/services/put");
        return endpoint;
    }

    // ERP 专用数据推送接口
    @Bean
    public Endpoint soapERPPutService(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpPutService);
        endpoint.publish("/services/erpput");
        return endpoint;
    }

    //  ERP 专用数据推送接口-库存数据
    @Bean
    public Endpoint soapErpIdsInvTransactionIntPutService(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpIdsInvTransactionIntPutService);
        endpoint.publish("/services/erpIdsInvTransactionIntput");
        return endpoint;
    }

    //  理赔信息写入接口定义
    @Bean
    public Endpoint ERPInsuranceServiceImpl(){
        EndpointImpl endpoint = new EndpointImpl(bus,erpInsuranceService);
        endpoint.publish("/services/ERPInsuranceService");
        return endpoint;
    }


/*  作用等同于cxf.path = /
    @Bean
    public ServletRegistrationBean cxfServletRegistration() {
        String urlMapping = "/*";
        ServletRegistrationBean registration = new ServletRegistrationBean(
                new CXFServlet(), urlMapping);
        registration.setLoadOnStartup(-1);
        return registration;
    }*/

    /**
     * 因配置了cxf.path=/,默认拦截所有请求，所以在这里指定被拦截的路径
     * 参考：https://www.dazhuanlan.com/ymzhangqiang/topics/1237244
     * @param applicationContext
     * @return
     */
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        DispatcherServlet servlet = new DispatcherServlet();
        servlet.setApplicationContext(applicationContext);

        String[] usersPatterns = new String[40];
        for (int i = 0; i < usersPatterns.length; i++) {
            usersPatterns[i] = "/users/"+(i+1)+"/13";
        }
        String[] otherPatterns = new String[]{
                "/list",
                "/css/bootstrap.css",
                "/plugins/font-awesome/css/font-awesome.min.css",
                "/css/style.css",
                "/plugins/elementui/index.css",
                "/js/axios-0.18.0.js",
                "/js/jquery.min.js",
                "/plugins/elementui/index.js",
                "/js/vue.js",
                "/js/axios.min.map",
                "/css/bootstrap.css.map",
                "/plugins/elementui/fonts/element-icons.woff",
                "/users",
                "/users/user",
                "/users/getToken",
                "/users/getUser",
                "/createToken",
                "/error",
                "/ids-pm-pro-tech/upload",
                "/ids-pm-pro-tech/projectCode",
                "/lnapp/get/LNFIN",
                "/lnapp/get/LNPLN"};
        String[] patterns = ArrayUtil.addAll(usersPatterns,otherPatterns);
        return new ServletRegistrationBean(servlet, patterns);
    }
}
